<template>
  <div>
    <div class="logo">
      <img src="@/assets/images/logo.png" alt="">
    </div>
    <van-form @submit="onSubmit">
      <van-field
        v-model="par.account"
        name="account"
        label="用户名"
        placeholder="用户名"
        :rules="[{ required: true, message: '请填写用户名' }]"
      />
      <van-field
        v-model="par.password"
        type="password"
        name="password"
        label="密码"
        placeholder="密码"
        :rules="[{ required: true, message: '请填写密码' }]"
      />
      <div style="margin: 16px;">
        <van-button
          round
          block
          type="info"
          native-type="submit"
        >提交</van-button>
      </div>
    </van-form>

  </div>
</template>

<script>
import { login } from "@/api/user";
export default {
  data() {
    return {
      title: "登录",
      par: {
         account: '',
         password: '',
      }
    }
  },
  metaInfo() {
    return {
      title: this.title,
    };
  },
  methods: {
    // 登录方式2
     onSubmit(values) {
     this.updata()
    },
    async updata() {
      const that = this
      try {
        const back = await login(this.par)
        if (back.status == 200) {
          this.$toast.success('登录成功');
          localStorage.setItem('token', back.data.token)
          localStorage.setItem('expires_time', back.data.expires_time)
          this.$store.commit('settoken', back.data.token)
          this.$router.back(-1)
        }
      } catch (err) {
        console.error(err)
      }
    },

  }
}
</script>

<style lang="less" scoped>
.logo{
  width:1.5rem;
  height: 1.5rem;
  background: #1989fa;
  margin: 1rem auto;
  border-radius: 0.1rem;
  img{
    width: 100%;
    height: 100%;
  }
}
</style>